package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AttributeTemplateMapper;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.AttributeTemplate;
import cn.tedu.csmall.product.service.IAttributeTemplateServrvice;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AttributeTemplateServiceImpl implements IAttributeTemplateServrvice {
    @Autowired
    private AttributeTemplateMapper mapper;

    @Override
    public void addNew(AttributeTemplateAddNewDTO attributeTemplateAddNewDTO) {
        int num = mapper.countByName(attributeTemplateAddNewDTO.getName());
        if (num > 0) {
            String message = "添加失败，该属性模板名称已被占用";
            throw new ServiceException(ServiceCode.ERR_CONFICT, message);
        }
        AttributeTemplate template = new AttributeTemplate();
        BeanUtils.copyProperties(attributeTemplateAddNewDTO, template);
        log.debug("即将擦入的数据：{}", template);
        mapper.insert(template);

    }
}
